package top.tagao.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import top.tagao.yygh.hosp.repository.DepartmentRepository;
import top.tagao.yygh.hosp.service.DepartmentService;
import top.tagao.yygh.model.hosp.Department;
import top.tagao.yygh.vo.hosp.DepartmentQueryVo;
import top.tagao.yygh.vo.hosp.DepartmentVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/4 9:59
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    //删除科室接口
    @Override
    public Boolean remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            //调用方法删除
            departmentRepository.deleteById(department.getId());
        }
        return true;
    }


    //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDepartmentTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result  = new ArrayList<DepartmentVo>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentAllList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> allDepartmentMap = departmentAllList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String,List<Department>> entry : allDepartmentMap.entrySet()){
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> departmentMapValues = entry.getValue();

            //封装大科室
            DepartmentVo departmentVoBig = new DepartmentVo();
            departmentVoBig.setDepcode(bigcode);
            departmentVoBig.setDepname(departmentMapValues.get(0).getBigname());


            //循环大科室获取小科室的 code 以及 name  然后 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : departmentMapValues) {
                DepartmentVo departmentVo2  = new DepartmentVo();
                departmentVo2 .setDepcode(department.getDepcode());
                departmentVo2 .setDepname(department.getDepname());
                children .add(departmentVo2);
            }
            //把小科室list集合放到大科室children里面
            departmentVoBig.setChildren(children);
            //放到最终result里面
            result.add(departmentVoBig);
        }
        return result;
    }

    /**
     * @date  根据医院编号,科室编号 查询科室名称
     */
    @Override
    public String getDepartmentName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    /**
     * @date  根据医院编号,科室编号. 查询科室
     * @param hoscode
     * @param depcode
     * @return top.tagao.yygh.model.hosp.Department
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

    //查询科室接口
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable
        Pageable pageable = PageRequest.of(page - 1, limit);

        //创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department,matcher);


        Page<Department> all = departmentRepository.findAll(example, pageable);


        return all;
    }



    //上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap 转换department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString,Department.class);


        //通过 hospCode  depCode 判断科室是否存在
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        //判断
        if(departmentExist != null) {
            department.setCreateTime(departmentExist.getCreateTime());
            department.setId(departmentExist.getId());
        } else {
            department.setCreateTime(new Date());
        }
        department.setIsDeleted(0);
        department.setUpdateTime(new Date());
        departmentRepository.save(department);
    }

}
